//
//  WeatherModel.m
//  KYProject
//
//  Created by Roman on 16/12/21.
//  Copyright © 2016年 Roman. All rights reserved.
//

#import "WeatherModel.h"


@implementation WeatherModel


- (void)getWeatherWithCityName:(NSString *)cityName complete:(void (^)(WeatherModel  *model, NSError *))completeBlock
{
    NSDictionary *paramDic = @{@"location":cityName,
                               @"output":@"json",
                               @"ak":@"17IvzuqmKrK1cGwNL6VQebF9"
                               };
    NSString *url = @"http://api.map.baidu.com/telematics/v3/weather";
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:url parameters:paramDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@",responseObject);
        if ([responseObject[@"status"] isEqualToString:@"success"]) {
            
            NSDictionary *resultsDict = [(NSArray *)responseObject[@"results"] firstObject];
            
            //获取天气数据
            NSDictionary *weatherDict = [(NSArray*)resultsDict[@"weather_data"] firstObject];
            

            
            [self yy_modelSetWithJSON:weatherDict];
           
            
        }
        completeBlock(self,nil);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"获取数据失败");
        
        completeBlock(nil,error);
    }];
}

@end
